﻿using System;
using System.Collections.Generic;
using Cinemachine;
using NodeCanvas.Tasks.Actions;
using UnityEngine;
using Yoozoo.Core.Common;
using Yoozoo.Core.Extensions.Engine;
using Yoozoo.Framework.Core;

namespace Gameplay.PVE.Survivor
{
    public class ControlManager: BattleSingleton<ControlManager>
    {
        private TeamGroup teamGroup;
        private string horAxisName = "Horizontal";
        private string verAxisName = "Vertical";

        private VirtualAnalog analog;
        private Vector3 lastDirection;

        private Vector3 startMousePosition;
        
        public void Update()
        {
            if (teamGroup == null)
            {
                teamGroup = StageManager.Instance.TeamGroup;
            }

            var direction = Vector3.zero;
#if UNITY_EDITOR
            if (Input.GetKeyDown(KeyCode.M))
            {
                StageManager.Instance.TeamGroup.CreateUnit(102);
            }
            
            if (Input.GetKeyDown(KeyCode.K))
            {
                Application.targetFrameRate = 10;
                var fpsMonitor = GameObject.FindObjectOfType<FpsLimitQualityListener>();
                if (fpsMonitor)
                {
                    fpsMonitor.enabled = false;
                }
            }
            if (analog == null)
            {
                analog = GameObject.FindObjectOfType<VirtualAnalog>();
            }
            if (Input.GetKeyDown(KeyCode.C))
            {
                SurvivorManager.Instance.Dispose();
            }
            
            if (Input.GetKey(KeyCode.W))
            {
                direction += Vector3.forward;
            }
            if (Input.GetKey(KeyCode.S))
            {
                direction += Vector3.back;
            }
            if (Input.GetKey(KeyCode.A))
            {
                direction += Vector3.left;
            }
            if (Input.GetKey(KeyCode.D))
            {
                direction += Vector3.right;
            }

            if (Input.GetKeyDown(KeyCode.N))
            {
                StageManager.Instance.StageToNext();
            }

            if (Input.GetKey(KeyCode.G))
            {
                if (startMousePosition == Vector3.zero)
                {
                    startMousePosition = Input.mousePosition;
                }
                else
                {
                    var offset = Input.mousePosition - startMousePosition;
                    direction = new Vector3(offset.x, 0, offset.y);
                }
            }
            else
            {
                startMousePosition = Vector3.zero;
            }

            if (analog)
            {
                if (direction != Vector3.zero)
                {
                    lastDirection = direction;
                    var length = direction.magnitude;
                    length = Mathf.Min(length, 50);
                    lastDirection = lastDirection.normalized;
                    analog.SetJoystickPosition(new Vector3(lastDirection.x * length,lastDirection.z * length,0));
                }
                else if (lastDirection != Vector3.zero)
                {
                    lastDirection = Vector3.zero;
                    analog.SetJoystickPosition(Vector3.zero);
                }
            }
#endif
            if (teamGroup != null)
            {
                var h = VirtualInput.GetAxis(horAxisName);
                var v = VirtualInput.GetAxis(verAxisName);
                if (h != 0 || v != 0)
                {
                    Vector3 joystickDirection = new Vector3(h, 0, v);
                    teamGroup.Move(joystickDirection.normalized);
                }
                else if (direction != Vector3.zero)
                {
                    teamGroup.Move(direction.normalized);
                }
                else
                {
                    teamGroup.StopMove();
                }
            }
        }

        public override void Dispose()
        {
            teamGroup = null;
        }
    }
}
